<template>
    <el-form ref="ruleFormRef"
        style="max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e4e7ed; border-radius: 5px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); background-color: #fff;"
        :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
        <div style="border : 1px solid #ccc; padding: 20px; border-radius: 10px">
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="ruleForm.roleName" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    添加
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">取消</el-button>
            </el-form-item>
        </div>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useCounterStore } from '@/stores/counter'
import { read, write } from "@/axios/axiosHelper"

const store = useCounterStore();

interface RuleForm {
    roleName: string
    userId: number
    createTime: string
    updateid: string
    updateTime: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    roleName: '',
    userId: store.counterStore.userId,
    createTime: "2025-02-24",
    updateid: '0',
    updateTime: "2025-02-24"
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    roleName: [
        { required: true, message: '请输入角色名称', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            write.post('/write/api/Role/AddRole', {
                roleName: ruleForm.roleName,
                userId: ruleForm.userId,
                createTime: ruleForm.createTime,
                updateid: ruleForm.updateid,
                updateTime: ruleForm.updateTime
            }).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("添加成功");
                }
                else if (res.data.code == 600) {
                    ElMessage.error("该角色已存在");
                } else {
                    ElMessage.error("添加失败");
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))
</script>